<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/26
 * Time: 10:25
 */

namespace common\business\mdm;


use common\consts\Consts;
use common\consts\MdmConsts;
use common\helpers\StringUtil;
use common\models\mdm\MdmDept;
use common\models\mdm\MdmEmployee;
use common\models\mdm\MdmOrg;
use common\models\mdm\MdmPosition;
use common\result\ActionResult;
use yii\helpers\Json;

class MdmApiHandler
{
    const wsdl_uri = 'http://10.0.1.14/uapws/service/mdmRceive?wsdl';
    /**
     * @param $params array|string
     * @return string
     */
    public function webServiceCallSync($params){
        try {
            $client = new \SoapClient(self::wsdl_uri);
            if (is_string($params)) {
                $paramsStr = $params;
            }
            if (is_array($params)) {
                $paramsStr = Json::encode($params);
            }
            $result = $client->mdmRceive([
                'sjson' => $paramsStr,
            ]);
            return $result->return;
        } catch (\Exception $e) {
            $exceptionResultArr = [
                MdmConsts::RESULT => Consts::NO,
                MdmConsts::MESSAGE => $e->getMessage(),
            ];
            return Json::encode($exceptionResultArr);
        }
    }

    /**
     * @param $mdmDept MdmDept
     * @return ActionResult
     */
    public function syncMdmDept($mdmDept){
        $params = [
            MdmConsts::INTER_CODE => MdmConsts::INT_CODE_DEPT,
        ];
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->PK_DEPARTMENT)){
            $params[MdmConsts::PK_DEPARTMENT] = $mdmDept->PK_DEPARTMENT;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->BILLCODE)){
            $params[MdmConsts::BILL_CODE] = $mdmDept->BILLCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->DEPARTMENTCODE)){
            $params[MdmConsts::DEPARTMENT_CODE] = $mdmDept->DEPARTMENTCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->DEPARTMENTNAME)){
            $params[MdmConsts::DEPARTMENT_NAME] = $mdmDept->DEPARTMENTNAME;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->SSORG)){
            $params[MdmConsts::S_S_ORG] = $mdmDept->SSORG;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->PK_DEF1)){
            $params[MdmConsts::PARENT_DEPT_PK] = $mdmDept->PK_DEF1;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->PHONE)){
            $params[MdmConsts::PHONE] = $mdmDept->PHONE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmDept->STARTDATE)){
            $params[MdmConsts::START_DATE] = $mdmDept->STARTDATE;
        }

        $resultStr = self::webServiceCallSync($params);
//        {"result":"1","message":"接收成功","billcode"主数据序号:"Dept002029","pk_department"主键:"0001A11000000001MD40","upts"同步时间:"2018-04-10 16:17:48"}
//        $resultStr = '{"result":"1","message":"接收成功","billcode":"Dept002029","pk_department":"0001A11000000001MD40","upts":"2018-04-10 16:17:48"}';
//        $resultArr = Json::decode($resultStr);
//        $isSuccess = $resultArr[MdmConsts::RESULT] == '1' ? true : false;
//        $message = $resultArr[MdmConsts::MESSAGE];

        $pos = strpos($resultStr, '"result":"1"');
        if ( $pos === false ){
            $isSuccess = false;
        }
        else $isSuccess = true;
        $message = $resultStr;

        return new ActionResult($isSuccess, $message);
    }

    /**
     * @param $mdmOrg MdmOrg
     * @return ActionResult
     */
    public function syncMdmOrg($mdmOrg){
        $params = [
            MdmConsts::INTER_CODE => MdmConsts::INT_CODE_ORG,
            MdmConsts::IS_NOT_USE => 'Y',
            MdmConsts::IS_HR_ORG_FLAG => 'Y',
        ];
        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->ORGCODE)){
            $params[MdmConsts::ORG_CODE] = $mdmOrg->ORGCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->ORGNAME)){
            $params[MdmConsts::ORG_NAME] = $mdmOrg->ORGNAME;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->BILLCODE)){
            $params[MdmConsts::BILL_CODE] = $mdmOrg->BILLCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->PK_MDM_ORG)){
            $params[MdmConsts::PK_MDM_ORG] = $mdmOrg->PK_MDM_ORG;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->VVDEF25)){
            $params[MdmConsts::HR_PARENT_ORG_FLAG] = $mdmOrg->VVDEF25;
        }

        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->YYZZCODE)){
            $params[MdmConsts::YYZZ_CODE] = $mdmOrg->YYZZCODE;
        }
        else {
            $params[MdmConsts::YYZZ_CODE] = StringUtil::gen15UniqueStr();
        }

        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->CSDJCODE)){
            $params[MdmConsts::CSDJ_CODE] = $mdmOrg->CSDJCODE;
        }
        else {
            $params[MdmConsts::CSDJ_CODE] = StringUtil::gen15UniqueStr();
        }

        if (!StringUtil::isNullOrWhiteSpace($mdmOrg->DSDJCODE)){
            $params[MdmConsts::DSDJ_CODE] = $mdmOrg->DSDJCODE;
        }
        else {
            $params[MdmConsts::DSDJ_CODE] = StringUtil::gen18UniqueStr();
        }

        $resultStr = self::webServiceCallSync($params);
//        {"result":"1","message":"接收成功","billcode"主数据序号:"Dept002029","pk_department"主键:"0001A11000000001MD40","upts"同步时间:"2018-04-10 16:17:48"}
//        $resultStr = '{"result":"1","message":"接收成功","upts":"2018-04-11 10:45:48"}';
//        $resultArr = Json::decode($resultStr);
//        $isSuccess = $resultArr[MdmConsts::RESULT] == '1' ? true : false;
//        $message = $resultArr[MdmConsts::MESSAGE];
        $pos = strpos($resultStr, '"result":"1"');
        if ( $pos === false ){
            $isSuccess = false;
        }
        else $isSuccess = true;
        $message = $resultStr;

        return new ActionResult($isSuccess, $message);
    }

    /**
     * @param $mdmPosition MdmPosition
     * @return ActionResult
     */
    public function syncMdmPosition($mdmPosition){
        $params = [
            MdmConsts::INTER_CODE => MdmConsts::INT_CODE_POSITION,
        ];
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->POSTCODE)){
            $params[MdmConsts::POST_CODE] = $mdmPosition->POSTCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->POSTNAME)){
            $params[MdmConsts::POST_NAME] = $mdmPosition->POSTNAME;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->BILLCODE)){
            $params[MdmConsts::BILL_CODE] = $mdmPosition->BILLCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->PK_POST)){
            $params[MdmConsts::PK_POST] = $mdmPosition->PK_POST;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->SSORG)){
            $params[MdmConsts::S_S_ORG] = $mdmPosition->SSORG;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->PK_DEF1)){
            $params[MdmConsts::SS_DEPT] = $mdmPosition->PK_DEF1;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmPosition->STARTDATE)){
            $params[MdmConsts::START_DATE] = $mdmPosition->STARTDATE;
        }

        $resultStr = self::webServiceCallSync($params);
//        {"result":"1","message":"接收成功","billcode"主数据序号:"Dept002029","pk_department"主键:"0001A11000000001MD40","upts"同步时间:"2018-04-10 16:17:48"}
//        $resultStr = '{"result":"1","message":"接收成功","upts":"2018-04-11 10:45:48"}';
        $resultArr = Json::decode($resultStr);
        $isSuccess = $resultArr[MdmConsts::RESULT] == '1' ? true : false;
        $message = $resultArr[MdmConsts::MESSAGE];
//        $pos = strpos($resultStr, '"result":"1"');
//        if ( $pos === false ){
//            $isSuccess = false;
//        }
//        else $isSuccess = true;
//        $message = $resultStr;

        return new ActionResult($isSuccess, $message);
    }

    /**
     * @param $mdmEmployee MdmEmployee
     * @return ActionResult
     */
    public function syncMdmEmployee($mdmEmployee){
        $params = [
            MdmConsts::INTER_CODE => MdmConsts::INT_CODE_EMPLOYEE,
        ];
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PK_PERSONNEL)){
            $params[MdmConsts::PK_PERSONNEL] = $mdmEmployee->PK_PERSONNEL;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PRESONCODE)){
            $params[MdmConsts::PERSON_CODE] = $mdmEmployee->PRESONCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PERSONNAME)){
            $params[MdmConsts::PERSON_NAME] = $mdmEmployee->PERSONNAME;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->BILLCODE)){
            $params[MdmConsts::BILL_CODE] = $mdmEmployee->BILLCODE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->SSORG)){
            $params[MdmConsts::S_S_ORG] = $mdmEmployee->SSORG;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PERSONSEX)){
            $params[MdmConsts::PERSON_SEX] = $mdmEmployee->PERSONSEX;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->VVDEF1)){
            $params[MdmConsts::PERSON_IDENTITY_CARD_TYPE] = $mdmEmployee->VVDEF1;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PERSONID)){
            $params[MdmConsts::PERSON_IDENTITY_NUM] = $mdmEmployee->PERSONID;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->BIRTHDATE)){
            $params[MdmConsts::BIRTH_DATE] = $mdmEmployee->BIRTHDATE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PERSONDEPART)){
            $params[MdmConsts::PERSON_DEPT] = $mdmEmployee->PERSONDEPART;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->PERSONPOST)){
            $params[MdmConsts::PERSON_POST] = $mdmEmployee->PERSONPOST;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->NOTUSEDATE)){
            $params[MdmConsts::NOT_USE_DATE] = $mdmEmployee->NOTUSEDATE;
        }
        if (!StringUtil::isNullOrWhiteSpace($mdmEmployee->NOTUSEMAN)){
            $params[MdmConsts::NOT_USE_MAN] = $mdmEmployee->NOTUSEMAN;
        }

        $resultStr = self::webServiceCallSync($params);
//        {"result":"1","message":"接收成功","billcode"主数据序号:"Dept002029","pk_department"主键:"0001A11000000001MD40","upts"同步时间:"2018-04-10 16:17:48"}
//        $resultStr = '{"result":"1","message":"接收成功","upts":"2018-04-11 10:45:48"}';
        $resultArr = Json::decode($resultStr);
        $isSuccess = $resultArr[MdmConsts::RESULT] == '1' ? true : false;
        $message = $resultArr[MdmConsts::MESSAGE];

        return new ActionResult($isSuccess, $message);
    }
}